<template>
  <nav class="navbar">
    <div class="navbar-container">
      <!-- Logo区域 -->
      <div class="navbar-logo">
        <router-link to="/">
          <span class="logo-icon">🌿</span>
          <span class="logo-text">自然智联</span>
        </router-link>
      </div>

      <!-- 导航菜单 -->
      <div class="navbar-menu">
        <router-link to="/" class="nav-item">首页</router-link>
        
        <div class="nav-dropdown" :class="{ active: isRouteActive('/shop') }">
          <span class="nav-item" @click="router.push('/shop/items')">二手商城</span>
          <div class="dropdown-content">
            <router-link to="/shop/items">商品列表</router-link>
            <router-link to="/shop/my-items">我的发布</router-link>
          </div>
        </div>

        <div class="nav-dropdown" :class="{ active: isRouteActive('/eco') }">
          <span class="nav-item" @click="router.push('/eco/record')">环保行动</span>
          <div class="dropdown-content">
            <router-link to="/eco/record">行为记录</router-link>
            <router-link to="/eco/stats">数据统计</router-link>
            <router-link to="/eco/trends">减碳趋势</router-link>
            <router-link to="/eco/ranking">排行榜</router-link>
          </div>
        </div>

        <div class="nav-dropdown" :class="{ active: isRouteActive('/garbage') }">
          <span class="nav-item" @click="router.push('/garbage/classify')">垃圾分类</span>
          <div class="dropdown-content">
            <router-link to="/garbage/classify">智能识别</router-link>
            <router-link to="/garbage/history">历史记录</router-link>
          </div>
        </div>

        <div class="nav-dropdown" :class="{ active: isRouteActive('/ai') }">
          <span class="nav-item" @click="router.push('/ai/qa')">AI智能体</span>
          <div class="dropdown-content">
            <router-link to="/ai/qa">知识问答</router-link>
            <router-link to="/ai/plan/generate">方案生成</router-link>
          </div>
        </div>

        <div class="nav-dropdown" :class="{ active: isRouteActive('/viz') }">
          <span class="nav-item" @click="router.push('/viz/environment')">数据可视化</span>
          <div class="dropdown-content">
            <router-link to="/viz/environment">环境数据</router-link>
            <router-link to="/viz/personal">个人贡献</router-link>
            <router-link to="/viz/dashboard">数据看板</router-link>
          </div>
        </div>

        <router-link to="/order/list" class="nav-item">我的订单</router-link>
      </div>

      <!-- 用户区域 -->
      <div class="navbar-user">
        <template v-if="isLoggedIn">
          <router-link to="/user/profile" class="user-link">
            <span class="user-icon">👤</span>
            <span>个人中心</span>
          </router-link>
          <button @click="handleLogout" class="logout-btn">退出登录</button>
        </template>
        <router-link v-else to="/login" class="login-btn">登录</router-link>
      </div>
    </div>
  </nav>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';

const router = useRouter();
const route = useRoute();
const isLoggedIn = ref(false);

// 检查路由是否激活
const isRouteActive = (path: string) => {
  return route.path.startsWith(path);
};

// 检查登录状态
const checkLoginStatus = () => {
  const token = localStorage.getItem('token');
  isLoggedIn.value = !!token;
};

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('token');
  localStorage.removeItem('userId');
  localStorage.removeItem('userAccount');
  isLoggedIn.value = false;
  router.push('/login');
};

// 组件挂载时检查登录状态
onMounted(() => {
  checkLoginStatus();
});
</script>

<style scoped>
.navbar {
  background: linear-gradient(135deg, #52C41A 0%, #389e0d 100%);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  position: sticky;
  top: 0;
  z-index: 1000;
  backdrop-filter: blur(10px);
}

.navbar-container {
  max-width: 1600px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 40px;
  height: 70px;
}

/* Logo区域 */
.navbar-logo a {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: white;
  font-size: 24px;
  font-weight: bold;
  gap: 12px;
  transition: transform 0.3s;
}

.navbar-logo a:hover {
  transform: scale(1.05);
}

.logo-icon {
  font-size: 32px;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

.logo-text {
  letter-spacing: 2px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* 导航菜单 */
.navbar-menu {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  justify-content: center;
}

.nav-item {
  color: white;
  text-decoration: none;
  padding: 10px 20px;
  border-radius: 8px;
  transition: all 0.3s;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  position: relative;
}

.nav-item::after {
  content: '';
  position: absolute;
  bottom: 5px;
  left: 50%;
  transform: translateX(-50%) scaleX(0);
  width: 60%;
  height: 2px;
  background-color: white;
  transition: transform 0.3s;
}

.nav-item:hover {
  background-color: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.nav-item:hover::after {
  transform: translateX(-50%) scaleX(1);
}

.nav-item.router-link-active {
  background-color: rgba(255, 255, 255, 0.25);
}

/* 下拉菜单激活状态 */
.nav-dropdown.active > .nav-item {
  background-color: rgba(255, 255, 255, 0.25);
}

.nav-dropdown.active > .nav-item::after {
  transform: translateX(-50%) scaleX(1);
}

/* 下拉菜单 */
.nav-dropdown {
  position: relative;
}

.dropdown-content {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: white;
  min-width: 180px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  border-radius: 8px;
  margin-top: 8px;
  padding: 8px 0;
  overflow: hidden;
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.nav-dropdown:hover .dropdown-content {
  display: block;
}

.dropdown-content a {
  color: #333;
  padding: 14px 20px;
  text-decoration: none;
  display: block;
  transition: all 0.2s;
  font-size: 15px;
  position: relative;
}

.dropdown-content a::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 4px;
  background-color: #52C41A;
  transform: scaleY(0);
  transition: transform 0.2s;
}

.dropdown-content a:hover {
  background-color: #f0f9ff;
  padding-left: 24px;
}

.dropdown-content a:hover::before {
  transform: scaleY(1);
}

.dropdown-content a.router-link-active {
  background-color: #e6f7ff;
  color: #52C41A;
  font-weight: 600;
  padding-left: 24px;
}

.dropdown-content a.router-link-active::before {
  transform: scaleY(1);
}

/* 用户区域 */
.navbar-user {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-link {
  display: flex;
  align-items: center;
  gap: 8px;
  color: white;
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 8px;
  transition: all 0.3s;
  font-size: 15px;
  font-weight: 500;
}

.user-link:hover {
  background-color: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.user-icon {
  font-size: 20px;
}

.login-btn {
  background-color: white;
  color: #52C41A;
  padding: 10px 24px;
  border-radius: 24px;
  text-decoration: none;
  font-weight: 600;
  transition: all 0.3s;
  font-size: 15px;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.login-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.25);
}

.logout-btn {
  background-color: rgba(255, 255, 255, 0.15);
  color: white;
  padding: 10px 24px;
  border-radius: 24px;
  border: 2px solid white;
  font-weight: 600;
  transition: all 0.3s;
  font-size: 15px;
  cursor: pointer;
  backdrop-filter: blur(5px);
}

.logout-btn:hover {
  background-color: white;
  color: #52C41A;
  transform: translateY(-3px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.25);
}

/* 响应式 */
@media (max-width: 1024px) {
  .navbar-container {
    padding: 0 20px;
  }
  
  .navbar-menu {
    gap: 4px;
  }
  
  .nav-item {
    padding: 8px 14px;
    font-size: 14px;
  }
}
</style>
